import 'package:flutter/material.dart';

class MyEra extends StatefulWidget {
  const MyEra({super.key});
  @override
  State<StatefulWidget> createState() => EraPage();
}

class EraPage extends State<MyEra> {
  @override
  Widget build(BuildContext context) {
    String imgUrl = 'assets/img/tiandi.jpg';
    return Container(
      padding: EdgeInsets.fromLTRB(20, 20, 20, 50),
      decoration: BoxDecoration(
        color: Colors.white,
        image: DecorationImage(image: AssetImage(imgUrl), fit: BoxFit.cover),
      ),
      child: Flex(
        direction: Axis.horizontal,
        children: [
          Expanded(flex: 20, child: FormBox(tagTit: '公元前')),
          Spacer(),
          Expanded(flex: 20, child: FormBox(tagTit: '公元')),
        ],
      ),
    );
  }
}

class FormBox extends StatefulWidget {
  final String tagTit;
  const FormBox({super.key, required this.tagTit});

  @override
  State<StatefulWidget> createState() => FormItem();
}

class FormItem extends State<FormBox> {
  final yearCtr = TextEditingController(); // 年份
  final skyCtr = TextEditingController(); // 天干
  final landCtr = TextEditingController(); // 地支
  final resCtr = TextEditingController(); // 结果

  //天干数组
  List tian = ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'];
  //地支数组
  List di = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'];
  // 生肖数组
  List sx = ['猴', '鸡', '狗', '猪', '鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊'];
  //验证输入是否为正数
  RegExp num = RegExp(r'^\d+$');

  //定义变量
  int n = 0, m = 0, s = 0, i = 0;

  void showResult(String value) {
    // print(value);
    yearCtr.text = value;
    skyCtr.text = value;
    landCtr.text = value;
    resCtr.text = value;

    int val = int.parse(value);
    if (widget.tagTit == '公元前') {
      //年干
      n = val % 10;
      //天干
      if (n < 8) {
        n = 8 - n;
      } else {
        n = 8 - n + 10;
      }

      //年支
      m = val % 12;
      //地支
      if (m < 10) {
        m = 10 - m;
      } else {
        m = 10 - m + 12;
      }
    } else {
      n = val % 10; //年干
      m = val % 12; //年支
      s = val % 12; //生肖

      //天干
      if (n > 3) {
        n = n - 3;
      } else {
        n = n - 3 + 10;
      }
      //地支
      if (m > 3) {
        m = m - 3;
      } else {
        m = m - 3 + 12;
      }
    }
    i = ((n + 10 - m) % 10 / 2 * 12 + m).toInt(); //甲子序数
    countYear();
  }

  void countYear() {
    //天干
    switch (n) {
      case 1:
        skyCtr.text = tian[n - 1];
        break;
      case 2:
        skyCtr.text = tian[n - 1];
        break;
      case 3:
        skyCtr.text = tian[n - 1];
        break;
      case 4:
        skyCtr.text = tian[n - 1];
        break;
      case 5:
        skyCtr.text = tian[n - 1];
        break;
      case 6:
        skyCtr.text = tian[n - 1];
        break;
      case 7:
        skyCtr.text = tian[n - 1];
        break;
      case 8:
        skyCtr.text = tian[n - 1];
        break;
      case 9:
        skyCtr.text = tian[n - 1];
        break;
      case 10:
        skyCtr.text = tian[n - 1];
        break;
    }

    //地支
    switch (m) {
      case 1:
        landCtr.text = di[m - 1];
        break;
      case 2:
        landCtr.text = di[m - 1];
        break;
      case 3:
        landCtr.text = di[m - 1];
        break;
      case 4:
        landCtr.text = di[m - 1];
        break;
      case 5:
        landCtr.text = di[m - 1];
        break;
      case 6:
        landCtr.text = di[m - 1];
        break;
      case 7:
        landCtr.text = di[m - 1];
        break;
      case 8:
        landCtr.text = di[m - 1];
        break;
      case 9:
        landCtr.text = di[m - 1];
        break;
      case 10:
        landCtr.text = di[m - 1];
        break;
      case 11:
        landCtr.text = di[m - 1];
        break;
      case 12:
        landCtr.text = di[m - 1];
        break;
    }

    //生肖
    switch (s) {
      case 0:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 1:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 2:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 3:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 4:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 5:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 6:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 7:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 8:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 9:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 10:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
      case 11:
        resCtr.text =
            '${skyCtr.text}${landCtr.text}${widget.tagTit == '公元前' ? '' : sx[s]}年，六十甲子序数：第$i';
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          Text(
            widget.tagTit,
            style: TextStyle(color: Colors.white, fontSize: 32),
          ),
          Container(
            margin: EdgeInsets.only(bottom: 10, top: 15),
            child: Flex(
              direction: Axis.horizontal,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                  child: Container(
                    padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                    height: 45,
                    transform: Matrix4.translationValues(2, 0, 0),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: Colors.blueGrey,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(6),
                        bottomLeft: Radius.circular(6),
                      ),
                    ),
                    child: Text(
                      '年份：',
                      textAlign: TextAlign.center,
                      style: TextStyle(color: Colors.white, fontSize: 18),
                    ),
                  ),
                ),
                Expanded(
                  flex: 5,
                  child: YearInp(
                    maxNum: 4,
                    isEdit: true,
                    inpCtr: yearCtr,
                    inpFun: showResult,
                  ),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(bottom: 10),
            child: Flex(
              direction: Axis.horizontal,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                  child: Container(
                    padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                    height: 45,
                    transform: Matrix4.translationValues(2, 0, 0),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: Colors.blueGrey,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(6),
                        bottomLeft: Radius.circular(6),
                      ),
                    ),
                    child: Text(
                      '天干：',
                      textAlign: TextAlign.center,
                      style: TextStyle(color: Colors.white, fontSize: 18),
                    ),
                  ),
                ),
                Expanded(
                  flex: 5,
                  child: YearInp(
                    maxNum: 15,
                    isEdit: false,
                    inpCtr: skyCtr,
                    inpFun: showResult,
                  ),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(bottom: 10),
            child: Flex(
              direction: Axis.horizontal,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                  child: Container(
                    padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                    height: 45,
                    transform: Matrix4.translationValues(2, 0, 0),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: Colors.blueGrey,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(6),
                        bottomLeft: Radius.circular(6),
                      ),
                    ),
                    child: Text(
                      '地支：',
                      textAlign: TextAlign.center,
                      style: TextStyle(color: Colors.white, fontSize: 18),
                    ),
                  ),
                ),
                Expanded(
                  flex: 5,
                  child: YearInp(
                    maxNum: 15,
                    isEdit: false,
                    inpCtr: landCtr,
                    inpFun: showResult,
                  ),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(bottom: 10),
            child: Flex(
              direction: Axis.horizontal,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                  child: Container(
                    width: 200,
                    padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                    height: 45,
                    transform: Matrix4.translationValues(2, 0, 0),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: Colors.blueGrey,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(6),
                        bottomLeft: Radius.circular(6),
                      ),
                    ),
                    child: Text(
                      '所求干支纪年为：',
                      textAlign: TextAlign.center,
                      style: TextStyle(color: Colors.white, fontSize: 18),
                    ),
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: YearInp(
                    maxNum: 15,
                    isEdit: false,
                    inpCtr: resCtr,
                    inpFun: showResult,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class YearInp extends StatefulWidget {
  final int? maxNum;
  final bool isEdit;
  final TextEditingController inpCtr;
  final Function(String) inpFun;

  const YearInp({
    super.key,
    required this.maxNum,
    required this.isEdit,
    required this.inpCtr,
    required this.inpFun,
  });

  @override
  State<StatefulWidget> createState() => InpItem();
}

class InpItem extends State<YearInp> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 45,
      child: TextField(
        onChanged: widget.inpFun,
        controller: widget.inpCtr,
        enabled: widget.isEdit,
        keyboardType: TextInputType.number,
        maxLength: widget.maxNum,
        style: TextStyle(
          color: Colors.black,
          fontSize: 18,
          fontWeight: FontWeight.w600,
        ),
        decoration: InputDecoration(
          contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
          border: OutlineInputBorder(),
          filled: true,
          fillColor: Colors.white,
          counterStyle: TextStyle(fontSize: 0),
        ),
      ),
    );
  }
}
